<template>
  <div id="detail" ref="detail" @scroll="detailScroll">
    <!-- 头部导航组件 -->
    <detail-nav-bar @titleClick="titleClick" ref="nav"></detail-nav-bar>
    <!-- 轮播图组件 -->
    <detail-swiper class="swiper d_jump" :banner="topImages"></detail-swiper>
    <!-- 基本信息 -->

    <detail-base-info :goods="goods"></detail-base-info>
    <!-- 店铺信息 -->

    <detail-shop-info :shop="shop"></detail-shop-info>
    <!-- 商品详情 -->

    <detail-images-info :images-info="detailInfo"></detail-images-info>
    <!-- 商品参数 -->
    <detail-param-info
      class="d_jump"
      ref="params"
      :param-info="paramInfo"
    ></detail-param-info>
    <!-- 商品评论 -->
    <detail-comment-info
      ref="comment"
      :commentInfo="commentInfo"
      class="d_jump"
    ></detail-comment-info>
    <!-- 相关推荐 -->

    <goods-list ref="recommend" class="d_jump" :goods="recommends" />
    <!-- 底部工具栏 -->
    <detail-bottom-bar @addToCart="addToCart"></detail-bottom-bar>
  </div>
</template>

<script>
// 导航栏组件
import DetailNavBar from "./childComps/DetailNavBar";
// 轮播图组件
import DetailSwiper from "./childComps/DetailSwiper";
// 基本信息组件
import DetailBaseInfo from "./childComps/DetailBaseInfo";

// 店铺信息组件
import DetailShopInfo from "./childComps/DetailShopInfo";
// 商品详情组件
import DetailImagesInfo from "./childComps/DetailImagesInfo";
// 参数组件
import DetailParamInfo from "./childComps/DetailParamInfo";
// 评论组件
import DetailCommentInfo from "./childComps/DetailCommentInfo";
// 相关推荐组件
import GoodsList from "components/content/goods/GoodsList";
// 底部工具栏
import DetailBottomBar from "./childComps/DetailBottomBar";

import {
  getDetail,
  Goods,
  Shop,
  GoodsParams,
  getRecommend
} from "../../network/detail";

export default {
  name: "Detail",
  components: {
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    DetailImagesInfo,
    DetailParamInfo,
    DetailCommentInfo,
    GoodsList,
    DetailBottomBar
  },
  created() {
    this.iid = this.$route.params.iid;
    // 获取详情数据
    this.getDetail();
    // 获取推荐数据
    getRecommend().then(res => {
      this.recommends = res.data.list;
    });
  },

  data() {
    return {
      iid: null,
      topImages: null,
      goods: {},
      shop: {},
      detailInfo: {},
      paramInfo: {},
      commentInfo: {},
      // 推荐数据
      recommends: [],
      themeTopY: [],
      currentIndex: 0
    };
  },

  methods: {
    // 加入购物车
    addToCart() {
      // console.log(11)
      const product = {};
      product.image = this.topImages[0];
      product.title = this.goods.title;
      product.desc = this.goods.desc;
      product.price = this.goods.realPrice;
      product.iid = this.iid;

      //添加到购物车  vuex
      this.$store.commit("addCart", product);
      // this.$store.dispatch("addCart", product);
    },
    // 监听滚动条
    detailScroll() {
      let scrollnum = this.$refs.detail.scrollTop;
      let params = this.$refs.params.$el.offsetTop - 44;
      let comment = this.$refs.comment.$el.offsetTop - 44;
      let recommend = this.$refs.recommend.$el.offsetTop - 44;
      // 获取当前详情容器的滚动距离 和 需要跳转页面offsetTop高度对比
      if (scrollnum >= recommend) {
        this.$refs.nav.currentIndex = 3;
      } else if (scrollnum >= comment) {
        this.$refs.nav.currentIndex = 2;
      } else if (scrollnum >= params) {
        this.$refs.nav.currentIndex = 1;
      } else if (scrollnum > 0 && scrollnum < params) {
        this.$refs.nav.currentIndex = 0;
      }
    },

    titleClick(index) {
      // 点击跳转对对应的位置
      let jump = document.querySelectorAll(".d_jump");
      // 获取需要滚动的距离
      let total = jump[index].offsetTop - 44;
      // Chrome
      this.$refs.detail.scrollTop = total;
      // 苹果浏览器
      window.pageYOffset = total;
    },

    async getDetail() {
      const res = await getDetail(this.iid);
      // console.log(res);
      const data = res.result;
      this.topImages = data.itemInfo.topImages;
      this.goods = new Goods(
        data.itemInfo,
        data.columns,
        data.shopInfo.services
      );
      // 店铺基本信息
      this.shop = new Shop(data.shopInfo);
      // 商品详情数据
      this.detailInfo = data.detailInfo;
      // 参数数据
      this.paramInfo = new GoodsParams(
        data.itemParams.info,
        data.itemParams.rule
      );
      // 评论数据
      if (data.rate.cRate !== 0) {
        this.commentInfo = data.rate.list[0];
      }
    }
  }
};
</script>

<style lang="less" scoped>
#detail {
  position: relative;
  z-index: 9;
  background: #fff;
  height: 100vh;
  overflow: auto;
  transition: all 1s;
}
.swiper {
  margin-top: 44px;
}
#tab-bar {
  display: none;
}
</style>
